﻿using SmartCity.ProcessEngine.API.Infrastructrue.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmartCity.ProcessEngine.API.Infrastructrue.Impelements
{
    /// <summary>
    ///仓储基类层
    /// </summary>
    /// <typeparam name="T">实体</typeparam>
    public class BaseRespository<T> : IBaseRespository<T> where T : class
    {
        /// <summary>
        /// 上下文
        /// </summary>
        private readonly MyDbcontext context;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="context">上下文</param>
        public BaseRespository(MyDbcontext context)
        {
            this.context = context;
        }

        public MyDbcontext DB => throw new NotImplementedException();

        /// <summary>
        /// 添加方法
        /// </summary>
        /// <param name="t">实体</param>
        /// <returns>返回任务</returns>
        public async Task<int> AddAsync(T t)
        {
            await context.Set<T>().AddAsync(t);
            return await context.SaveChangesAsync();
        }
        /// <summary>
        /// 批量添加方法
        /// </summary>
        /// <param name="t">实体集合</param>
        /// <returns>返回批量任务</returns>
        public async Task<int> AddRege(IEnumerable<T> t)
        {
            await context.Set<T>().AddRangeAsync(t);    
            return await context.SaveChangesAsync();    
        }

        /// <summary>
        /// 查询方法
        /// </summary>
        /// <returns>返回任务</returns>
        public IQueryable<T> GetAll()
        {
            return context.Set<T>().AsQueryable();
        }
        /// <summary>
        /// 修改方法
        /// </summary>
        /// <param name="t">实体</param>
        /// <returns>返回任务</returns>
        public async Task<int> UpdateAsync(T t)
        {
            context.Set<T>().Update(t);
            return await context.SaveChangesAsync();
        }
        /// <summary>
        /// 批量更新
        /// </summary>
        /// <param name="t">实体集合</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> UpdateRege(IEnumerable<T> t)
        {
            context.Set<T>().UpdateRange(t);
            return await context.SaveChangesAsync();
        }
    }
}
